package com.application.lucky.widget.dialog;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.application.lucky.R;

public class SignleButtonDialog extends Dialog implements View.OnClickListener {


    private TextView mTvMessage;
    private TextView mTvDialogRightBt;

    public SignleButtonDialog(@NonNull Context context) {
        super(context, R.style.dialog);


        View view = LayoutInflater.from(context).inflate(R.layout.dialog_single_button_view, null);
        initView(view);
        setContentView(view);

    }

    private String userId;

    public void setUserId(String userId) {
        this.userId = userId;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_dialog_left_bt:
                dismiss();
                break;
            case R.id.tv_dialog_right_bt:
                if (isDissmiss) {
                    dismiss();
                }
                break;

        }
    }


    private void initView(View view) {

        mTvMessage = (TextView) view.findViewById(R.id.tv_message);
        mTvDialogRightBt = (TextView) view.findViewById(R.id.tv_dialog_right_bt);

    }

    public void setmTvMessage(String s) {
        if (mTvMessage != null) {
            mTvMessage.setText(s);
        }
    }

    public void setmTvMessage(int s) {
        if (mTvMessage != null) {
            mTvMessage.setText(s);
        }
    }

    public static class Build {
        private final SignleButtonDialog dialog;
        private Context context;

        public Build(Context context) {
            this.context = context;
            dialog = new SignleButtonDialog(context);
        }

        public Build setMessage(String s) {
            dialog.setTvMessage(s);
            return this;
        }

        public Build disMiss(boolean isDissmiss) {
            dialog.setDismiss(isDissmiss);
            return this;
        }

        public Build setMessage(int s) {
            dialog.setmTvMessage(s);
            return this;
        }


        public Build setRightButton(String s, BaseDialogClickListener rightListener) {
            dialog.setRightListener(s, rightListener);
            return this;
        }


        public SignleButtonDialog create() {
            return dialog;
        }
    }

    private boolean isDissmiss;

    private void setDismiss(boolean isDissmiss) {
        this.isDissmiss = isDissmiss;
    }

    private void setRightListener(String s, final BaseDialogClickListener rightListener) {
        if (mTvDialogRightBt != null) {
            mTvDialogRightBt.setText(s);
            mTvDialogRightBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (rightListener != null) {
                        rightListener.click();
                    }
                }
            });
        }
    }

    private void setTvMessage(String s) {
        if (mTvMessage != null) {
            mTvMessage.setText(s);
        }
    }

    public interface BaseDialogClickListener {
        void click();
    }


}

